﻿package mortal.game.view.group
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.debug.*;
    import mortal.game.cache.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.copy.groupCopy.data.*;

    public class GroupCache extends Object
    {
        private var _waitingPlayers:Array;
        private var _players:Array;
        private var _captain:SEntityId;
        private var _groupId:SEntityId;
        private var _nearTeamList:Array;
        private var _inviteList:Array;
        private var _applyList:Array;
        private var _groupName:String;
        private var _sgroup:SGroup;
        public var memberInvite:Boolean = false;
        public var autoEnter:Boolean = false;
        private var _groupCopyInviteList:Vector.<SGroupCopyInvite>;
        public var groupCopyEnterNum:int;
        public var groupCopyAutoEnter:Boolean = true;
        public var combat:int = -1;
        public var passWord:String = "";
        public var groupCopyTemporaryIndex:String = "m1";
        public var grouCopyDefaultIndex:String = "m1";
        public var groupCopyLeaveOperData:GroupCopyLeaveOperData;
        public var lastGroupId:int;

        public function GroupCache()
        {
            this._waitingPlayers = [];
            this._players = [];
            this._nearTeamList = [];
            this._inviteList = [];
            this._applyList = [];
            this._groupCopyInviteList = new Vector.<SGroupCopyInvite>;
            return;
        }// end function

        private function sortCaptain(param1:SGroupPlayer, param2:SGroupPlayer) : int
        {
            if (EntityUtil.isSelf(param1.player.entityId))
            {
                return -1;
            }
            return 1;
        }// end function

        public function get isInGroup() : Boolean
        {
            return this._players.length > 0;
        }// end function

        public function get isInNomalGrop() : Boolean
        {
            return this._players.length > 0 && this._sgroup.copyCode == 0;
        }// end function

        public function get selfInfo() : SGroupPlayer
        {
            var _loc_1:* = new SGroupPlayer();
            _loc_1.player = new SPublicPlayer();
            _loc_1.player.name = Cache.instance.role.playerInfo.name;
            _loc_1.player.level = Cache.instance.role.entityInfo.level;
            _loc_1.player.VIP = Cache.instance.role.playerInfo.VIP;
            _loc_1.player.camp = Cache.instance.role.playerInfo.camp;
            _loc_1.player.career = Cache.instance.role.entityInfo.career;
            _loc_1.player.combat = Cache.instance.role.entityInfo.combat;
            _loc_1.player.entityId = Cache.instance.role.entityInfo.entityId;
            return _loc_1;
        }// end function

        public function get isCaptain() : Boolean
        {
            if (!this._captain || !Cache.instance.role.entityInfo.entityId)
            {
                return false;
            }
            if (this.isInGroup && this._captain && EntityUtil.equal(Cache.instance.role.entityInfo.entityId, this._captain))
            {
                return true;
            }
            return false;
        }// end function

        public function isCaptainById(param1:SEntityId) : Boolean
        {
            return EntityUtil.equal(param1, this._captain);
        }// end function

        public function entityIsInGroup(param1:SEntityId) : Boolean
        {
            var _loc_3:SEntityId = null;
            if (param1 == null)
            {
                return false;
            }
            var _loc_2:int = 0;
            while (_loc_2 < this._players.length)
            {
                
                _loc_3 = (this.players[_loc_2] as SGroupPlayer).player.entityId;
                if (param1 && _loc_3)
                {
                    if (param1.id == _loc_3.id && param1.typeEx == _loc_3.typeEx && param1.typeEx2 == _loc_3.typeEx2)
                    {
                        return true;
                    }
                }
                _loc_2++;
            }
            return false;
        }// end function

        public function getSGroupPlayerBySEntityId(param1:SEntityId) : SGroupPlayer
        {
            var _loc_2:SGroupPlayer = null;
            var _loc_4:SEntityId = null;
            if (param1 == null)
            {
                return null;
            }
            var _loc_3:int = 0;
            while (_loc_3 < this._players.length)
            {
                
                _loc_4 = (this.players[_loc_3] as SGroupPlayer).player.entityId;
                if (_loc_4)
                {
                    if (param1.id == _loc_4.id && param1.typeEx == _loc_4.typeEx && param1.typeEx2 == _loc_4.typeEx2)
                    {
                        _loc_2 = this.players[_loc_3] as SGroupPlayer;
                    }
                }
                _loc_3++;
            }
            return _loc_2;
        }// end function

        public function addInviteList(param1:SGroupOper) : void
        {
            var _loc_2:SGroupOper = null;
            for each (_loc_2 in this._inviteList)
            {
                
                if (EntityUtil.equal(param1.fromEntityId, _loc_2.fromEntityId))
                {
                    _loc_2 = param1;
                    return;
                }
            }
            this._inviteList.push(param1);
            return;
        }// end function

        public function addGroupCopyInviteList(param1:SGroupCopyInvite) : void
        {
            var _loc_2:SGroupCopyInvite = null;
            for each (_loc_2 in this._groupCopyInviteList)
            {
                
                if (EntityUtil.equal(_loc_2.groupOper.fromEntityId, param1.groupOper.fromEntityId))
                {
                    _loc_2 = param1;
                    return;
                }
            }
            this._groupCopyInviteList.push(param1);
            return;
        }// end function

        public function addApplyList(param1:SGroupOper) : void
        {
            var _loc_2:SGroupOper = null;
            for each (_loc_2 in this._applyList)
            {
                
                if (EntityUtil.equal(_loc_2.fromEntityId, param1.fromEntityId))
                {
                    _loc_2 = param1;
                    return;
                }
            }
            this._applyList.push(param1);
            return;
        }// end function

        public function disbanGroup() : void
        {
            var clearLastGroupId:Function;
            var cd:ICDData;
            clearLastGroupId = function () : void
            {
                lastGroupId = 0;
                return;
            }// end function
            ;
            if (this.isInCopyGroup)
            {
                this.lastGroupId = this._sgroup.groupId.id;
                cd = Cache.instance.cd.registerCDData(CDDataType.publicCD, "GroupCopyApply");
                cd.totalTime = 10000;
                cd.startCoolDown();
                cd.addFinishCallback(clearLastGroupId);
            }
            this._players = new Array();
            this._captain = null;
            this._groupId = null;
            this._sgroup = null;
            var _loc_2:Boolean = false;
            this.memberInvite = false;
            this.autoEnter = _loc_2;
            this._groupName = "";
            return;
        }// end function

        public function get waitingPlayers() : Array
        {
            return this._waitingPlayers;
        }// end function

        public function set waitingPlayers(param1:Array) : void
        {
            this._waitingPlayers = param1;
            return;
        }// end function

        public function get players() : Array
        {
            return this._players;
        }// end function

        public function set players(param1:Array) : void
        {
            this._players = param1;
            this._players.sort(this.sortCaptain);
            return;
        }// end function

        public function get captain() : SEntityId
        {
            return this._captain;
        }// end function

        public function set captain(param1:SEntityId) : void
        {
            this._captain = param1;
            return;
        }// end function

        public function get groupId() : SEntityId
        {
            return this._groupId;
        }// end function

        public function set groupId(param1:SEntityId) : void
        {
            this._groupId = param1;
            return;
        }// end function

        public function get inviteList() : Array
        {
            return this._inviteList;
        }// end function

        public function set inviteList(param1:Array) : void
        {
            this._inviteList = param1;
            return;
        }// end function

        public function get groupCopyInviteList() : Vector.<SGroupCopyInvite>
        {
            return this._groupCopyInviteList;
        }// end function

        public function get applyList() : Array
        {
            return this._applyList;
        }// end function

        public function set applyList(param1:Array) : void
        {
            this._applyList = param1;
            return;
        }// end function

        public function get nearTeamList() : Array
        {
            return this._nearTeamList;
        }// end function

        public function set nearTeamList(param1:Array) : void
        {
            var _loc_2:int = 0;
            if (this.isInGroup)
            {
                while (_loc_2 < param1.length)
                {
                    
                    if ((param1[_loc_2] as SGroup).groupId.id == this._groupId.id)
                    {
                        param1.splice(_loc_2, 1);
                    }
                    _loc_2++;
                }
            }
            this._nearTeamList = param1;
            return;
        }// end function

        public function get groupName() : String
        {
            return this._groupName;
        }// end function

        public function set groupName(param1:String) : void
        {
            this._groupName = param1;
            return;
        }// end function

        public function set sgroup(param1:SGroup) : void
        {
            this._sgroup = param1;
            return;
        }// end function

        public function get sgroup() : SGroup
        {
            return this._sgroup;
        }// end function

        public function get isInCopyGroup() : Boolean
        {
            return this.isInGroup && this._sgroup.copyCode > 0;
        }// end function

        public function get isInDefenseCopyGroup() : Boolean
        {
            if (this._sgroup == null)
            {
                return false;
            }
            var _loc_1:* = this._sgroup.copyCode;
            var _loc_2:* = CopyConfig.instance.getCopyInfoByCode(this._sgroup.copyCode);
            if (_loc_2)
            {
                if (_loc_2.group == 112 || _loc_2.group == 128 || _loc_2.group == 138)
                {
                    return true;
                }
            }
            return false;
        }// end function

        public function get isInGroupTowerCopy() : Boolean
        {
            var _loc_1:TCopy = null;
            if (this.isInCopyGroup)
            {
                _loc_1 = CopyConfig.instance.getCopyInfoByCode(this._sgroup.copyCode);
                if (_loc_1)
                {
                    return _loc_1.type == ECopyType._ECopyTypeTower;
                }
                return false;
            }
            else
            {
                return false;
            }
        }// end function

        public function get isTeamMateInCopy() : Boolean
        {
            var _loc_1:Boolean = false;
            var _loc_2:SGroup = null;
            var _loc_3:Array = null;
            var _loc_4:SGroupPlayer = null;
            if (this.isInCopyGroup)
            {
                _loc_2 = this._sgroup;
                _loc_3 = CopyConfig.instance.getCopyInfoByCode(_loc_2.copyCode).maps.split("#");
                for each (_loc_4 in _loc_2.players)
                {
                    
                    if (_loc_3.indexOf(_loc_4.player.mapId.toString()) >= 0)
                    {
                        _loc_1 = true;
                        Log.debug("队友的地图id", _loc_4.player.mapId);
                        break;
                    }
                }
            }
            return _loc_1;
        }// end function

    }
}
